﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// qt headers
#include <QScrollBar>
#include <QEvent>

// custom headers
#include "CustomScroller.h"
#include "GbmpNew.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    CustomScroller::CustomScroller(QWidget *parent) :
        QScrollArea(parent)
    {
        Init();
    }

    CustomScroller::~CustomScroller()
    {

    }

    bool CustomScroller::eventFilter(QObject * watched, QEvent * event)
    {
        if (!watched || !event)
        {
            return QScrollArea::eventFilter(watched, event);
        }

        if (m_bStretchHorizontal)
        {
            if (watched == viewport())
            {
                if (event->type() == QEvent::Type::Resize)
                {
                    if (widget())
                    {
                        widget()->setFixedWidth(viewport()->width());
                    }
                }
            }
        }

        return QScrollArea::eventFilter(watched, event);
    }

    void CustomScroller::SetStretchHorizontal(const bool & stretchHorizontal)
    {
        if (m_bStretchHorizontal != stretchHorizontal)
        {
            m_bStretchHorizontal = stretchHorizontal;            
        }
    }

    void CustomScroller::Init()
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(viewport(), L"viewport()为空", L"GMEP", L"2024-01-30");
        viewport()->installEventFilter(this);

        setStyleSheet(
            "QScrollArea{"
            "border:none;"
            "background:rgba(245, 245, 245, 255);"
            "}"
        );

        DBG_WARN_AND_RETURN_VOID_UNLESS(verticalScrollBar(), L"verticalScrollBar()为空", L"GMEP", L"2024-01-30");
        verticalScrollBar()->setStyleSheet(
            "QScrollBar:vertical{"
                "width:8px;"
                "background:rgba(0,0,0,0%);"
                "margin:0px,0px,0px,0px;"
                "padding-top:0px;"
                "padding-bottom:0px;"
                "padding-left:1px;"
                "padding-right:1px;"
            "}"
            "QScrollBar::handle:vertical{"
                "width:6px;"
                "background:rgba(35,40,50,20%);"
                "border-radius:2px;"
                "min-height:20;"
            "}"
            "QScrollBar::handle:vertical:hover{"
                "width:6px;"
                "background:rgba(35,40,50,40%);"
                " border-radius:2px;"
                "min-height:20;"
            "}"
            "QScrollBar::add-line:vertical{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_down_8.png);"
                "subcontrol-position:bottom;"
            "}"
            "QScrollBar::sub-line:vertical{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_up_8.png);"
                "subcontrol-position:top;"
            "}"
            "QScrollBar::add-line:vertical:hover{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_down_8.png);"
                "subcontrol-position:bottom;"
            "}"
            "QScrollBar::sub-line:vertical:hover{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_up_8.png);"
                "subcontrol-position:top;"
            "}"
            "QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical{"
                "background:rgba(255,255,255,10%);"
                "border-radius:4px;"
            "}"
        );

        DBG_WARN_AND_RETURN_VOID_UNLESS(horizontalScrollBar(), L"horizontalScrollBar()为空", L"GMEP", L"2024-01-30");
        horizontalScrollBar()->setStyleSheet(
            "QScrollBar:horizontal{"
            "height:8px;"
            "background:rgba(0,0,0,0%);"
            "margin:0px,0px,0px,0px;"
            "padding-top:1px;"
            "padding-bottom:1px;"
            "padding-left:0px;"
            "padding-right:0px;"
            "}"
            "QScrollBar::handle:horizontal{"
            "height:6px;"
            "background:rgba(35,40,50,20%);"
            "border-radius:2px;"
            "min-width:20;"
            "}"
            "QScrollBar::handle:horizontal:hover{"
            "height:6px;"
            "background:rgba(35,40,50,40%);"
            "border-radius:2px;"
            "min-width:20;"
            "}"
            "QScrollBar::add-line:horizontal{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_down_8.png);"
            "subcontrol-position:left;"
            "}"
            "QScrollBar::sub-line:horizontal{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_up_8.png);"
            "subcontrol-position:right;"
            "}"
            "QScrollBar::add-line:horizontal:hover{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_down_8.png);"
            "subcontrol-position:left;"
            "}"
            "QScrollBar::sub-line:horizontal:hover{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_up_8.png);"
            "subcontrol-position:right;"
            "}"
            "QScrollBar::add-page:horizontal,QScrollBar::sub-page:horizontal{"
            "background:rgba(255,255,255,10%);"
            "border-radius:4px;"
            "}"
        );
    }

}